package com.ruoyi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.series.Line;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jfree.chart.*;

import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.DefaultCategoryDataset;
import org.apache.commons.io.FileUtils;
import org.jfree.data.xy.DefaultXYDataset;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.File;
import java.io.IOException;




@RestController
@Api(tags = "测试")
@RequestMapping(value = "/ray/demo/")
public class Demo {


    @SaIgnore
    @ApiOperation("生成折线图")
    @GetMapping("test")
    public void test() {
        // 创建数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1, "Series 1", "Category 1");
        dataset.addValue(2, "Series 1", "Category 2");
        dataset.addValue(3, "Series 1", "Category 3");
        dataset.addValue(2.5, "Series 1", "Category 4");

        // 创建折线图
        JFreeChart chart = ChartFactory.createLineChart(
                "Line Chart", // 图表标题
                "Category", // X轴标签
                "y轴", // Y轴标签
                dataset // 数据集
        );

        // 保存图表为PNG文件
        File outputFile = new File("line_chart.png");
        try {
            ChartUtils.saveChartAsPNG(outputFile, chart, 800, 600);
            System.out.println("Chart saved successfully.");
        } catch (IOException e) {
            System.out.println("Error saving chart: " + e.getMessage());
        }
    }

    private Font labelFont = new Font("宋体", Font.PLAIN, 12);

    @SaIgnore
    @ApiOperation("生成曲线图")
    @GetMapping("test2")
    public String test2() {





        //创建主题样式
                StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
                standardChartTheme.setExtraLargeFont(new Font("微软雅黑",Font.BOLD,20));
        //设置图例的字体
                standardChartTheme.setRegularFont(new Font("微软雅黑",Font.PLAIN,15));
        //设置轴向的字体
                standardChartTheme.setLargeFont(new Font("微软雅黑",Font.PLAIN,15));
        //应用主题样式
                ChartFactory.setChartTheme(standardChartTheme);

        // 创建数据集
        DefaultXYDataset dataset = new DefaultXYDataset();

        // 生成第一条曲线数据 女性
        double[][] data1 = {{15,20,25,30,35,40,45,50,55,60,65,70,75}, {0.340,0.381,0.412,0.409,0.396,0.376,0.354,0.334,0.322,0.312,0.304,0.298,0.294}};
        dataset.addSeries("女", data1);

        // 生成第二条曲线数据
        double[][] data2 = {{15,20,25,30,35,40,45,50,55,60,65,70,75}, {0.386,0.427,0.458,0.455,0.442,0.422,0.400,0.380,0.368,0.358,0.350,0.344,0.340}};
        dataset.addSeries("男", data2);

        // 创建图表
        JFreeChart chart = ChartFactory.createXYLineChart("骨密度指数表", "年龄(岁)", "骨密度cm²", dataset);


//        ChartPanel chartPanel = new ChartPanel(chart);
//        chartPanel.setBackground(new Color(250,0,0,50));



        XYPlot  plot   =(XYPlot) chart.getPlot();
        plot.setBackgroundAlpha(0.2F);//背景透明

        // 设置网格横线颜色
        plot.setRangeGridlinePaint(Color.gray);
        // 设置网格横线大小
        plot.setRangeGridlineStroke(new BasicStroke(0.2F));

        ValueAxis axisX = plot.getRangeAxis();//获取柱状
            axisX.setAutoRange(false);
//        axisX.setLowerBound(0.5F);
//        axisX.setUpperBound(2.0F);

        ValueAxis axisY = plot.getDomainAxis(); //获取X轴，注意这里使用的是domainAxis，而不是rangeAxis
//        axisY.setLowerBound(15); //设置X轴最小值为1
//        axisY.setUpperBound(75); //设置X轴最大值为75，根据需要你可以调整这个值
//
//        plot.setDataset(dataset);

        // 保存图表为JPEG文件
        try {
            ChartUtils.saveChartAsJPEG(new File("qq496631085.jpg"), chart, 500, 400);


        } catch (IOException e) {
            e.printStackTrace();
            return "生成图表失败";
        }

        return "生成图表成功";
    }
}
